//公共方法
var Vue = require('vue')

function requestAjax(url,param,dataForm = null,callBack,failBack=null) {
        let sessionId = getCookie('sessionId'),
            userId = getCookie('userId')
        let sendUrl = url+'?sessionId='+ sessionId;
        for(let item in dataForm){
            sendUrl = sendUrl + "&" + item + '=' + dataForm[item];
        }
        Vue.default.http.post(sendUrl,param,{emulateJSON: true,credentials: true}).then(res => {
            callBack(res.data)
        }, res => {
            if(failBack){
                failBack(res.data)
            }
        });
}

//读取cookie：
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");　　
    return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : null;
}

function dateFormate(num,type='date'){
    var d=new Date(num);
    var year=d.getFullYear();
    var day=d.getDate();
    var month=+d.getMonth()+1;
    var hour=d.getHours();
    var minute=d.getMinutes();
    var second=d.getSeconds();
    if(type=='date'){
        var f=year+"-"+formate(month)+"-"+formate(day)+" "+formate(hour)+":"+formate(minute)+":"+formate(second);
    }else if(type=='birth'){
        var f=year+"-"+formate(month)+"-"+formate(day)
    }
    
    function formate(d){
        return d>9?d:'0'+d;
    }
    return f;
}

function returnNum(num){
    var str;

    str = num;
    if(num>9999&&num<=99999999){
        str = (num/10000).toFixed(2) + '万'
    }
    if(num>99999999){
        str = (num/100000000).toFixed(1) + '亿'
    }
    return str?str:0;
}   


//设置cookie：
function setCookie(name, value){
    let Days = 1,
    exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function formateMoney(n) {
    if (typeof n != 'number') return new Error('formateMoney require a number')
    n = n.toLocaleString()
    const arr = n.split('.')
    let afterDot =  arr[1] || ''
    afterDot = (afterDot + '00').slice(0, 2)
    return `${arr[0]}.${afterDot}`
}

function checkSocialCreditCode(Code) {  
    var patrn = /^[0-9A-Z]+$/;  
    //18位校验及大写校验  
    if ((Code.length != 18) || (patrn.test(Code) == false)) {  
      console.info("不是有效的统一社会信用编码！");  
      return false;  
    }  
    else {  
      var Ancode;//统一社会信用代码的每一个值  
      var Ancodevalue;//统一社会信用代码每一个值的权重   
      var total = 0;  
      var weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];//加权因子   
      var str = '0123456789ABCDEFGHJKLMNPQRTUWXY';  
      //不用I、O、S、V、Z   
      for (var i = 0; i < Code.length - 1; i++) {  
        Ancode = Code.substring(i, i + 1);  
        Ancodevalue = str.indexOf(Ancode);  
        total = total + Ancodevalue * weightedfactors[i];  
        //权重与加权因子相乘之和   
      }  
      var logiccheckcode = 31 - total % 31;  
      if (logiccheckcode == 31) {  
        logiccheckcode = 0;  
      }  
      var Str = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y";  
      var Array_Str = Str.split(',');  
      logiccheckcode = Array_Str[logiccheckcode];  
    
    
      var checkcode = Code.substring(17, 18);  
      if (logiccheckcode != checkcode) {  
        console.info("不是有效的统一社会信用编码！");  
        return false;  
      }else{  
        console.info("yes");  
      }  
      return true;  
    }  
}  


module.exports = {
  requestAjax,
  dateFormate,
  getCookie,
  setCookie,
  returnNum,
  formateMoney,
  checkSocialCreditCode
}
